package AI;


import GAME.game;
import TDALista.BoundaryViolationException;
import TDALista.DoubleLinkedList;
import TDALista.EmptyListException;
import TDALista.InvalidPositionException;
import TDALista.Position;
import TDALista.PositionList;
import weaponrelated.*;
import powerup.*;

public class powerupAI extends Thread {
	protected PositionList<powerup> powerups;
	protected game juego;
	
	public powerupAI(game g){
		powerups=new DoubleLinkedList<powerup>();
		juego=g;
	}
	public void insertpowerup(powerup b){
		powerups.addLast(b);
		juego.getpanel().add(b);
		
	}
	public void run() {
		Position<powerup>[] powerupmuertos=(Position<powerup>[])new Position[100];
		int cantelim=0;
		while(juego.getplayer().isalive()){
			cantelim=0;
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {}
			
			if(!powerups.isEmpty()){
				for(Position<powerup> p:powerups.positions()){
					if(p.element().getY()>juego.getHeight()){
						juego.getContentPane().remove(p.element());
						powerupmuertos[cantelim]=p;
						cantelim++;
					}else{
						p.element().setLocation(p.element().getX(),p.element().getY()+1 );
						int x=(int)(juego.getplayer().getLocation().getX()+(juego.getplayer().getWidth()/2));
						int y=(int)(juego.getplayer().getLocation().getY());
						boolean cond1=((x>p.element().getLocation().getX()) && (x<(p.element().getLocation().getX()+35)));
						boolean cond2=((y>p.element().getLocation().getY()) && (y<(p.element().getLocation().getY()+35)));
						if(cond1 && cond2){
							juego.getpanel().remove(p.element());
							p.element().activarpowerup();
							powerupmuertos[cantelim]=p;
							cantelim++;
						}
				
					}
				}
				for(int i=0;i<cantelim;i++){
					try {
						powerups.remove(powerupmuertos[i]);
					} catch (InvalidPositionException e) {}
					powerupmuertos[i]=null;
					
				}
			}
			
		}
	
				
				
				
}
			
				
			
}
			
